package com.project.service.impl;

import com.project.bean.Car;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public void addMan(ManBean man, List<Car> carList) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.addMan(man);
        mapper.addCarList(man.getId(), carList);

        session.commit();
        session.close();
    }

    @Override
    public void delManCasecade(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.delManCasecade(id);

        session.commit();
        session.close();
    }

    @Override
    public void delManSetNull(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.delManSetNull(id);

        session.commit();
        session.close();
    }

    @Override
    public List<ManBean> findAll() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public ManBean findById(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        ManBean manBean = mapper.findById(id);

        session.close();
        return manBean;
    }
}
